package com.woniuxy.service.impl;

import com.woniuxy.dao.QuestionInventoryMapper;
import com.woniuxy.dao.QuestionOptionMapper;
import com.woniuxy.dao.QuestionTextMapper;
import com.woniuxy.dao.QuestionTopicMapper;
import com.woniuxy.entity.*;
import com.woniuxy.service.ReadingQuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ReadingQuServiceImpl implements ReadingQuService {

    @Resource
    QuestionInventoryMapper questionInventoryMapper;

    @Resource
    QuestionTopicMapper questionTopicMapper;

    @Resource
    QuestionTextMapper questionTextMapper;

    @Resource
    QuestionOptionMapper questionOptionMapper;


    @Override
    public Map<Object, Object> findAllReadingQu() {
//        text --> topic1 --> topic1.option1 --> topic1.option2 ... --> topic2 --> topic2.option1 --> ...
//        将阅读题的所有实体类封装至map，k为对应部分的名称
        Map<Object, Object> item = new HashMap();
//        找到对应题库中的所有topic
        List<QuestionTopic> topicByInvenId = questionTopicMapper.findTopicByInvenId(5);
//        将topic的编号与option的编号对应
        item.put("topics", topicByInvenId);
//        遍历题库中的topic
        for (QuestionTopic topic : topicByInvenId) {
//            将得到的topic的id作为option的tid进行sql查询
            List<QuestionOption> questionOptions = questionOptionMapper.findOptByTid(topic.getId());
//            如果已经插入过options,则将新查询的list添加到原有的value的list中
            if (item.keySet().contains("options")) {
                List<QuestionOption> options = (List<QuestionOption>) item.get("options");
                options.addAll(questionOptions);
                item.put("options", options);
            } else {
                item.put("options", questionOptions);
            }
//                item.put("text" + topic.getQuestionText().getId(), topic.getQuestionText().getDec());
        }
        return item;
    }


    public Map<String, Object> findAllInfo(Integer id) {
        Map<String, Object> allInfo = new HashMap<>();
        if (id == -1) {
            List<QuestionInventory> allFatherSubject = questionInventoryMapper.findAllFatherSubject();
            allInfo.put("fatherSub", allFatherSubject);
        } else {
            List<QuestionInventory> sonSubject = questionInventoryMapper.findAllSonSubject(id);
            allInfo.put("sonSub", sonSubject);
        }
        return allInfo;
    }

    @Override
    public ResponseEntity addReadingQu(Map<String, Object> param) {
        Boolean flag = false;
        Integer invenId = (Integer) param.get("InvenId");
        String text = (String) param.get("text");
        ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) param.get("readingQu");
        Integer teid = questionTopicMapper.getMaxTeid();
        teid++;
        for (Map<String, Object> topicDetail : list) {
            QuestionTopic topic = new QuestionTopic();
            topic.setTypeid(5);
            topic.setIid(invenId);
            topic.setText(text);
            topic.setTeid(teid);
            topic.setTopic((String) topicDetail.get("topic"));
            questionTopicMapper.insertUseIfStatement(topic);
            Integer topicId = topic.getId();
            Set<String> keySet = topicDetail.keySet();
            Map<String, Object> optionMap = OptionMatch(topicDetail, keySet);
            for (String option : optionMap.keySet()) {
                QuestionOption questionOption = new QuestionOption();
                questionOption.setTopicId(topicId);
                questionOption.setOpt(option);
                if ((Boolean)optionMap.get(option)){
                    questionOption.setStatus("y");
                }else {
                    questionOption.setStatus("n");
                }
                questionOptionMapper.insert(questionOption);
            }
            flag = true;
        }
        if (flag) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    public Map<String, Object> OptionMatch(Map<String, Object> topicDetail, Set<String> keySet) {
        Map<String, Object> optionMap = new HashMap<>();
        for (String topicDetailKey : keySet) {
            if (topicDetailKey.contains("option")) {
                Integer opId = Integer.valueOf(topicDetailKey.substring(6));
                for (String statusKey : keySet) {
                    if (statusKey.contains("Status")) {
                        Integer statusId = Integer.valueOf(statusKey.substring(2, 3));
                        if (opId == statusId) {
                            optionMap.put((String) topicDetail.get(topicDetailKey),topicDetail.get(statusKey));
                        }
                    }
                }
            }
        }
        return optionMap;
    }
}
